package com.mibao.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

	private static final int DB_VERSION = 1;
	private static final String DATABASE_NAME = "mibao.db";

	private static final String CREATETABLE_USER_INFO = "CREATE TABLE IF NOT EXISTS mibao_user_info(id INTEGER PRIMARY KEY AUTOINCREMENT," + "password VARCHAR(20) NOT NULL)";
	private static final String CREATETABLE_USER_FRIEND = "CREATE TABLE IF NOT EXISTS mibao_user_friend(id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20) NOT NULL,phone VARCHAR(15) NOT NULL)";
	private static final String CREATETABLE_USER_APP = "CREATE TABLE IF NOT EXISTS mibao_user_app(id INTEGER PRIMARY KEY AUTOINCREMENT,appName VARCHAR(20) NOT NULL,packageName VARCHAR(20),appIcon BLOB)";
	
	private static final String CREATETABLE_USER_CALL_RECORD = "CREATE TABLE IF NOT EXISTS mibao_user_record(id INTEGER PRIMARY KEY AUTOINCREMENT,phone VARCHAR(15),time timestamp,flag SMALLINT)";
	private static final String CREATETABLE_USER_MESSAGE = "CREATE TABLE IF NOT EXISTS mibao_user_message(id INTEGER PRIMARY KEY AUTOINCREMENT,phone VARCHAR(15),message Text,flag SMALLINT)";
    
	public DBHelper(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version);
	}

	public DBHelper(Context context) {
		this(context, DATABASE_NAME, DB_VERSION);
	}

	public DBHelper(Context context, String name) {
		this(context, name, DB_VERSION);
	}

	public DBHelper(Context context, String name, int version) {
		this(context, name, null, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATETABLE_USER_INFO);
		db.execSQL(CREATETABLE_USER_FRIEND);
		db.execSQL(CREATETABLE_USER_APP);
		db.execSQL(CREATETABLE_USER_CALL_RECORD);
		db.execSQL(CREATETABLE_USER_MESSAGE);
	}
    
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
		System.out.println("update database");
		this.deleteDB(db);  
        this.onCreate(db);  
	}
    private void deleteDB(SQLiteDatabase db) {  
        db.execSQL("drop table if exists " + DATABASE_NAME);  
    }  
}
